package com.cupcorn.balapkarung.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.balapkarung.GameDefineText;
import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.Ratio;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.display.DisplayText;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameState;
import com.cupcorn.lib.game.GameText;

public class StateAbout extends GameState implements GameDefine, GameDefineText
{
	
	DisplaySprite displaySprite;
	DisplayText displayText;
	
	boolean upinside;
	
	final int animateStep = MAIN_MENU_ANIMATION_STEP;
	int animateCount;
	
	boolean ready, quitingSubmenu;
	
	Entity layerMenu;
	
	boolean controlledTextMoving;
	float yWhenDown, yMoveTo, textPos;
	
	public StateAbout(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displaySprite = new DisplaySprite(engine, GAME_MENU_ABOUT);
		displayText = new DisplayText(engine, engine.displayFont, GameDefineText.ABOUT_NAME);
		layerMenu = new Entity();
		
		for(int i=0; i<GAME_MENU_ABOUT.length; i++)
		{
			displaySprite.newSprite(i, engine.hud, false);
		}
		
		for(int i=0; i<GameDefineText.ABOUT_NAME.length; i++)
		{
			displayText.text(i);
		}
		
		
		layerMenu.attachChild(displaySprite.getSprite(ABOUT_BINGKAI));
		displaySprite.getSprite(ABOUT_BINGKAI).addRectangularShape(displaySprite.getSprite(ABOUT_LOGO), 5, 5, Anchor.TOP_LEFT);
		
		displaySprite.getSprite(ABOUT_LOGO).addText(displayText.text[ABOUT_TITLE], 100, 0, Anchor.TOP_LEFT);
	}
	
	public void init()
	{
		upinside = false;
		
		animateCount = 0;
		ready = false;
		quitingSubmenu = false;
		
		engine.hud.attachChild(layerMenu);
		layerMenu.setY(- GameEngine.CAMERA_HEIGHT);
		
		controlledTextMoving = false;
		yWhenDown = 0;
		yMoveTo = 0;
		textPos = 0;
	}
	
	public void update(float deltaTime)
	{
		if(quitingSubmenu)
		{
			if(animateBackTransition())
			{
				engine.nextState(STATE_MAIN_MENU, iDefines.MESSAGE_STATE_UPDATE);
				removeAllObject();
			}
			
			return;
		}
		
		if(ready)
		{
			
			if(displayText.text[SPR_1].getY() < GameEngine.CAMERA_HEIGHT/7)
			{
				reinitPosition();
			}
			
			for(int i = PL_ID; i< ABOUT_NAME.length; i++)
			{
				if(isInBox(displayText.text[i]))
				{
					displayText.text[i].setVisible(true);
				}
				else
				{
					displayText.text[i].setVisible(false);
				}
			}
			
			if(controlledTextMoving)
			{
				
			}
			else
			{
				
				float yMoveSpeed = 1;
				for(int i = PL_ID; i< ABOUT_NAME.length; i++)
				{
					displayText.text[i].setY(displayText.text[i].getY()-yMoveSpeed);
				}
			}
			
			
			upinside = false;
			
			return;
		}
		
		
		if(animateTransition())
		{
			ready = true;
		}
	}
	
	boolean isInBox(GameText text)
	{
		if(text.getY() > GameEngine.CAMERA_HEIGHT/6 && text.getY() < GameEngine.CAMERA_HEIGHT-GameEngine.CAMERA_HEIGHT/3 )
		{
			return true;
		}
		return false;
	}
	
	public void exit()
	{	
		
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if(quitingSubmenu || !ready)
		{
			return false;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{	
		if(!ready || quitingSubmenu)
		{
			return;
		}
		if(touchEvent.isActionUp() && !upinside)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if(pSceneTouchEvent.isActionUp())
		{
			if(pTouchArea.equals(displaySprite.getSprite(0)))
			{
				upinside = true;
				controlledTextMoving = false;
				yWhenDown = 0;
				yMoveTo = 0;
				textPos = 0;
			}
		}
		
		if(pSceneTouchEvent.isActionDown())
		{
			if(pTouchArea.equals(displaySprite.getSprite(ABOUT_BINGKAI)))
			{
				Utils.TRACE("DOWN ...."+pSceneTouchEvent.getY());
				controlledTextMoving = true;
				yWhenDown = pSceneTouchEvent.getY();
				textPos = displayText.text[PL_ID].getY();
				Utils.TRACE("text pos nya "+textPos);
			}
		}
		
		if(pSceneTouchEvent.isActionMove())
		{
			if(pTouchArea.equals(displaySprite.getSprite(ABOUT_BINGKAI)))
			{
				Utils.TRACE("MOVE ...."+pSceneTouchEvent.getY());
				yMoveTo = pSceneTouchEvent.getY() - yWhenDown;
				Utils.TRACE("MOVE ...."+yMoveTo);
				setPosition(yMoveTo);
				Utils.TRACE("text pos nya "+displayText.text[GD_ID].getY());
			}
		}
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{	
		
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	private void removeAllObject()
	{
		EngineLock lock = engine.getEngine().getEngineLock();
		for(int i = PL_ID; i< ABOUT_NAME.length; i++)
		{
			displayText.text[i].detachSelf();
		}
		lock.lock();
		layerMenu.detachSelf();
		lock.unlock();
	}
	
	boolean animateTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() + StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() + StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() + StateMainMenu.farMove);
		
		if(animateCount == animateStep)
		{
			attachText();
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	void reinitPosition()
	{
		float yIncrement = 450;
		float ySpace = 50;
		float yIDSpace = 70;
		float yPerSectionSpace = 90;
		for(int i = PL_ID; i< ABOUT_NAME.length; i++)
		{
			displayText.text[i].setPosition(displaySprite.getSprite(ABOUT_BINGKAI), 0, yIncrement, Anchor.TOP_CENTER, Ratio.HEIGHT);
			if(i == PL_ID || i == GD_ID || i == PRG_ID || i == ART_ID || i == SND_ID || i == SPR_ID)
			{
				yIncrement += yIDSpace;
			}
			else if(i == PL_1 || i == GD_2 || i == PRG_4 || i == ART_2 || i == SND_1 || i == SPR_1)
			{
				yIncrement += yPerSectionSpace;
			}
			else
			{
				yIncrement += ySpace;
			}
		}
	}
	
	void setPosition(float moveOffset)
	{
		float yIncrement = (textPos*(GameEngine.MASTER_HEIGHT/GameEngine.CAMERA_HEIGHT)) + moveOffset;
		float ySpace = 50;
		float yIDSpace = 70;
		float yPerSectionSpace = 90;
		for(int i = PL_ID; i< ABOUT_NAME.length; i++)
		{
			displayText.text[i].setPosition(displaySprite.getSprite(ABOUT_BINGKAI), 0, yIncrement, Anchor.TOP_CENTER, Ratio.HEIGHT);
			if(i == PL_ID || i == GD_ID || i == PRG_ID || i == ART_ID || i == SND_ID || i == SPR_ID)
			{
				yIncrement += yIDSpace;
			}
			else if(i == PL_1 || i == GD_2 || i == PRG_4 || i == ART_2 || i == SND_1 || i == SPR_1)
			{
				yIncrement += yPerSectionSpace;
			}
			else
			{
				yIncrement += ySpace;
			}
		}
	}
	
	void attachText()
	{
		float yIncrement = 480;
		float ySpace = 50;
		float yIDSpace = 70;
		float yPerSectionSpace = 90;
		for(int i = PL_ID; i< ABOUT_NAME.length; i++)
		{
			displaySprite.getSprite(ABOUT_BINGKAI).addText(displayText.text[i], 0, yIncrement, Anchor.TOP_CENTER);
			if(i == PL_ID || i == GD_ID || i == PRG_ID || i == ART_ID || i == SND_ID || i == SPR_ID)
			{
				yIncrement += yIDSpace;
			}
			else if(i == PL_1 || i == GD_2 || i == PRG_4 || i == ART_2 || i == SND_1 || i == SPR_1)
			{
				yIncrement += yPerSectionSpace;
			}
			else
			{
				yIncrement += ySpace;
			}
		}
	}
	
	boolean animateBackTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() - StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() - StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() - StateMainMenu.farMove);
		
		if(animateCount == animateStep)
		{
			return true;
		}
		
		animateCount++;
		
		return false;
	}
}
